package com.example.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.deletelistviewitemdemo.R;
import com.example.view.SlideAdapter.DeleteItemListener;

public class DeleteListViewMainActivity extends Activity {

	private ListViewCompat mListView;

	private List<MessageItem> mMessageItems = new ArrayList<MessageItem>();

	private SlideAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview_delete_main);
		initView();
	}

	public void initView() {
		mListView = (ListViewCompat) findViewById(R.id.list);

		for (int i = 0; i < 20; i++) {
			MessageItem item = new MessageItem();
			item.setTv_content("我是第  " + i + " 个Item");
			mMessageItems.add(item);
		}
		// 创建适配器对象
		adapter = new SlideAdapter(DeleteListViewMainActivity.this,
				mMessageItems);
		// 删除监听
		adapter.setDeleteItemListener(new DeleteItemListener() {
			@Override
			public void deleteItem(View view, int position) {
				mMessageItems.remove(position);
				adapter.notifyDataSetChanged();
				Toast.makeText(getApplicationContext(),
						"删除了第  " + position + " item", 0).show();
			}
		});
		// 设置适配器
		mListView.setAdapter(adapter);
		// 点击item事件
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), "点击了=" + position, 0)
						.show();

			}

		});
	}

}
